package com.create404.suishoupai.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.create404.suishoupai.dto.LikedNewsDto;
import com.create404.suishoupai.dto.PageVo;
import com.create404.suishoupai.entity.NewsLike;
import com.create404.suishoupai.service.INewsLikeService;
import com.create404.suishoupai.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author create404
 * @since 2021-07-17
 */
@Api(tags = "新闻点赞接口")
@RestController
@RequestMapping("/news-like")
public class NewsLikeController {
    @Autowired
    INewsLikeService iNewsLikeService;

    @PostMapping("/addNewsLike")
    public Result addNewsLike(String newsId, String employeeId) {
        NewsLike newsLike = new NewsLike();
        newsLike.setNewsId(newsId);
        newsLike.setEmployeeId(employeeId);
        newsLike.setCreateTime(LocalDateTime.now());
        newsLike.setUpdateTime(LocalDateTime.now());
        newsLike.setTime(LocalDateTime.now());
        boolean result = iNewsLikeService.save(newsLike);
        if (result) {
            return Result.ok("点赞成功");
        } else {
            return Result.error("点赞失败");
        }
    }

    @DeleteMapping("/deleteNewsLike")
    public Result deleteNewsLike(String newsId, String employeeId) {

        boolean result = iNewsLikeService.remove(this.iNewsLikeService.deleteNewsLiked(newsId, employeeId));
        if (result) {
            return Result.ok("取消点赞成功");
        } else {
            return Result.error("取消点赞成功");
        }
    }

    @GetMapping("/isNewsLiked")
    public Result isNewsLiked(String newsId, String employeeId) {
        return Result.ok(this.iNewsLikeService.isNewsLiked(newsId, employeeId).size());
    }


    @PostMapping("/getLikedNews")
    public Result getLikedNews(@RequestBody PageVo pageVo) {
        String employeeId = pageVo.getEmployeeId();
        Integer currentPage = Integer.valueOf(pageVo.getPageNum());
        Integer pageSize = Integer.valueOf(pageVo.getPageSize());
        Page<LikedNewsDto> likedNewsDtoPage = new Page<LikedNewsDto>(currentPage, pageSize);
        IPage<LikedNewsDto> page = iNewsLikeService.getLikedNews(likedNewsDtoPage, employeeId);
        return Result.ok(page);
    }


}
